package com.imooc.config;

import com.imooc.base.RabbitMqConfig;
import com.imooc.entity.mo.MessageMO;
import com.imooc.enums.MessageEnum;
import com.imooc.service.MsgService;
import com.imooc.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class RabbitMqCustomer {
    @Autowired
    private MsgService msgService;
    @RabbitListener(queues = {RabbitMqConfig.QUEUE_NAME})
    public void watchQueue(String payload, Message message){
        log.info("receive message:{}",payload);
        MessageMO messageMO = JsonUtils.jsonToPojo(payload, MessageMO.class);
        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
        if(routingKey.equalsIgnoreCase("sys.msg."+ MessageEnum.FOLLOW_YOU.enValue)){
            log.info("异步解耦用户关注消息收发");
            msgService.createMsg(messageMO.getFromUserId(),messageMO.getToUserId(), MessageEnum.FOLLOW_YOU.type,null);
        }else if(routingKey.equalsIgnoreCase("sys.msg."+ MessageEnum.LIKE_VLOG.enValue)){
            log.info("异步解耦视频点赞消息收发");
            msgService.createMsg(messageMO.getFromUserId(),messageMO.getToUserId(), MessageEnum.LIKE_VLOG.type,messageMO.getMsgContent());
        }else if(routingKey.equalsIgnoreCase("sys.msg."+MessageEnum.LIKE_COMMENT.enValue)){
            log.info("异步解耦评论点赞消息收发");
            msgService.createMsg(messageMO.getFromUserId(),messageMO.getToUserId(), MessageEnum.LIKE_COMMENT.type,messageMO.getMsgContent());
        }else if(routingKey.equalsIgnoreCase("sys.msg."+MessageEnum.COMMENT_VLOG.enValue)){
            log.info("异步解耦评论视频消息收发");
            msgService.createMsg(messageMO.getFromUserId(),messageMO.getToUserId(), MessageEnum.COMMENT_VLOG.type,messageMO.getMsgContent());
        }else if(routingKey.equalsIgnoreCase("sys.msg."+MessageEnum.UN_LIKE_VLOG)){
            log.info("异步解耦取消点赞视频消息收发");
            msgService.unlikeThisVlog(messageMO.getFromUserId(),messageMO.getToUserId());
        }else if(routingKey.equalsIgnoreCase("sys.msg."+MessageEnum.UN_LIKE_COMMENT.enValue)){
            log.info("异步解耦取消点赞评论消息收发");
            msgService.unLikeThiComment(messageMO.getFromUserId(),messageMO.getToUserId());
        }
    }
}
